import 'package:easy_refresh/easy_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/inspec/inspec_query_plan_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/inspec_task_page/received_task_page/received_task_controller.dart';

/// 已领取任务
class ReceivedTaskPage extends StatelessWidget {
  ReceivedTaskPage({super.key});

  final controller = Get.put(ReceivedTaskController());

  /// 弹窗是选择方案
  _selectPlan() {
    CustomSelectFunction.selectMultipleBottomSheet<InspecQueryPlanEntity>(
      title: '请选择',
      isMultiple: false,
      dataList: controller.alreadyPlan,
      checkedList: controller.selectAlreadyPlan != null ? [controller.alreadyPlan[controller.selectAlreadyPlan!]] : [],
      onCallBack: (value) {
        if (value.isNotEmpty) {
          controller.selectAlreadyPlan = controller.alreadyPlan.indexOf(value[0]);

        }
        controller.update(['dialog']);
      },
    );
  }

  /// 方案选择
  _taskSubmit() {
    Get.dialog(GetBuilder(
      id: 'dialog',
      init: controller,
      builder: (_) {
        return Container(
          alignment: Alignment.center,
          child: Container(
            width: AppDimens.sw - 24.w,
            height: 280.w,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(20.w)),
              gradient: const LinearGradient(
                  //渐变位置
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  //渐变颜色[始点颜色, 结束颜色]
                  colors: [
                    Color(0xffE7EAFF),
                    Color(0xffffffff),
                    Color(0xffffffff),
                    Color(0xffffffff),
                  ]),
            ),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 16.w),
                  child: Column(children: [
                    AppGaps.vGap20,
                    Text(
                      '方案选择',
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 18.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    AppGaps.vGap24,
                    Column(
                      children: [
                        Row(
                          children: [
                            Text(
                              '方案名称',
                              style: TextStyle(
                                fontSize: 14.sp,
                                color: Colors.black,
                              ),
                            ),
                            AppGaps.hGap12,
                            Expanded(
                              child: Container(
                                height: 32.w,
                                padding: EdgeInsets.symmetric(horizontal: 10.w),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.circular(8),
                                  border: Border.all(
                                    color: AppColors.bg_color_grey,
                                  ),
                                ),
                                child: Row(
                                  children: [
                                    Text(controller.selectAlreadyPlan != null ? controller.alreadyPlan[controller.selectAlreadyPlan!].getShowName() : '请选择方案名称',
                                        style: TextStyle(
                                          fontSize: 14.sp,
                                          color: AppColors.font_color_gray,
                                        )),
                                    const Spacer(),
                                    const Icon(Icons.arrow_drop_down_outlined, color: AppColors.font_color_gray),
                                  ],
                                ),
                              ).click(_selectPlan),
                            )
                          ],
                        ),
                        AppGaps.vGap12,
                        Text('提交后执行任务、已领取任务将自动更新为该方案任务。',style: TextStyle(
                          color: const Color(0xFFFC9900),
                          fontSize: 12.sp,
                        )),
                        AppGaps.vGap50,
                        SizedBox(
                          width: 270.w,
                          height: 40.w,
                          child: ElevatedButton(
                            style: ButtonStyle(
                              backgroundColor: MaterialStateProperty.all(const Color(0xff5C6DFF)),
                            ),
                            onPressed: controller.planSelect,
                            child: Text(
                              '提交',
                              style: TextStyle(fontSize: 16.sp, color: Colors.white),
                            ),
                          ),
                        ),
                        AppGaps.vGap20,
                      ],
                    )
                  ]),
                ),
                Positioned(
                    top: 8,
                    right: 8,
                    child: IconButton(
                      icon: Container(
                        width: 30.w,
                        height: 30.w,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.all(Radius.circular(15.w)),
                        ),
                        child: Icon(
                          Icons.close,
                          color: Colors.grey,
                          size: 21.w,
                        ),
                      ),
                      onPressed: Get.back,
                    )),
              ],
            ),
          ),
        );
      },
    ));
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Container(
          padding: EdgeInsets.only(left: 14.w, right: 14.w),
          child: Column(
            children: [
              controller.isClickEdit ? Container() :  Container(
                height: 48.w,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Container(
                      padding: const EdgeInsets.only(top: 4, bottom: 4, right: 8, left: 8).w,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(14),
                          border: Border.all(
                            color: AppColors.font_color_blue,
                          )),
                      child: Text('撤销当前方案',
                          style: TextStyle(
                            color: AppColors.font_color_blue,
                            fontSize: 12.sp,
                          )),
                    ).clickOnce(controller.cancelCurrentPlan),
                    Container(
                      padding: const EdgeInsets.only(top: 4, bottom: 4, right: 8, left: 8).w,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(14),
                          border: Border.all(
                            color: AppColors.font_color_blue,
                          )),
                      child: Text('编辑当前方案',
                          style: TextStyle(
                            color: AppColors.font_color_blue,
                            fontSize: 12.sp,
                          )),
                    ).clickOnce(controller.editCurrentPlan),
                    Container(
                      padding: const EdgeInsets.only(top: 4, bottom: 4, right: 8, left: 8).w,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(14),
                        color: AppColors.font_color_blue,
                      ),
                      child: Text('方案选择',
                          style: TextStyle(
                            color: AppColors.font_color_while,
                            fontSize: 12.sp,
                          )),
                    ).click(_taskSubmit),
                  ],
                ),
              ),
              Visibility(
                  visible: controller.isClickEdit,
                  child: Container(
                    height: 48.w,
                    margin: EdgeInsets.only(top: 8.w),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(8),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: [
                        Container(
                          padding: const EdgeInsets.only(top: 3, bottom: 3, right: 30, left: 30).w,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(18),
                              border: Border.all(
                                color: AppColors.font_color_blue,
                              )),
                          child: Text('取消',
                              style: TextStyle(
                                color: AppColors.font_color_blue,
                                fontSize: 16.sp,
                              )),
                        ).clickOnce(controller.cancelTask),
                        Container(
                          padding: const EdgeInsets.only(top: 4, bottom: 4, right: 40, left: 40).w,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(18),
                            color: AppColors.font_color_blue,
                          ),
                          child: Text('提交',
                              style: TextStyle(
                                color: AppColors.font_color_while,
                                fontSize: 16.sp,
                              )),
                        ).click(controller.submitTask),
                      ],
                    ),
                  )
              ),
              Expanded(
                child: Container(
                  padding: EdgeInsets.only(left: 12.w, top: 14.w, right: 20.w, bottom: 40.w),
                  margin: EdgeInsets.only(bottom: 20.w, top: 10.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Stack(
                    children: [
                      Container(
                        width: 1.w,
                        height: AppDimens.sh,
                        margin: EdgeInsets.only(
                          left: 4.w,
                          top: 10.w,
                        ),
                        color: AppColors.font_color_blue,
                      ),
                      EasyRefresh(
                        onRefresh: controller.allTask,
                        child: ListView.builder(
                          itemCount: controller.timeList.length,
                          itemBuilder: (cxt, index) {
                            return Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                SizedBox(
                                  height: 30.w,
                                  child: Row(
                                    children: [
                                      Container(
                                        width: 10.w,
                                        height: 10.w,
                                        padding: const EdgeInsets.all(2),
                                        decoration: BoxDecoration(color: AppColors.font_color_blue, borderRadius: BorderRadius.circular(90)),
                                        child: Container(
                                          decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(4)),
                                        ),
                                      ),
                                      AppGaps.hGap6,
                                      Text(
                                        '${controller.timeList[index].time}:00',
                                        style: TextStyle(
                                          fontSize: 15.sp,
                                          fontWeight: FontWeight.w500,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                ListView.builder(
                                  shrinkWrap: true,
                                  physics: const NeverScrollableScrollPhysics(),
                                  padding: EdgeInsets.only(left: 16.w, bottom: 10.w),
                                  itemCount: controller.timeList[index].tasks.length,
                                  itemBuilder: (cxt, i) {
                                    return SizedBox(
                                      height: 25.w,
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        children: [
                                          Expanded(
                                            child: Text(
                                              '${i + 1}.${controller.timeList[index].tasks[i].taskName}',
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                color: AppColors.font_color_gray,
                                                fontSize: 13.sp,
                                              ),
                                            ),
                                          ),
                                          Visibility(
                                            visible: controller.isClickEdit,
                                            child: Container(
                                              height: 25.w,
                                              padding: const EdgeInsets.only(left: 5, right: 5),
                                              child: Text(
                                                '移除',
                                                style: TextStyle(
                                                  color: AppColors.font_color_blue,
                                                  fontSize: 13.sp,
                                                ),
                                              ),
                                            ).click(() {
                                              controller.removeTask(index, i);
                                            }),
                                          )
                                        ],
                                      ),
                                    );
                                  },
                                ),
                              ],
                            );
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
